﻿using Dapper;
using Helper;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 

namespace BLL
{
    public class Sys_User_BLL: BaseBLL
    {
        #region 单例模式


        // 定义一个静态变量来保存类的实例
        private static Sys_User_BLL uniqueInstance;

        // 定义一个标识确保线程同步
        private static readonly object locker = new object();

        // 定义私有构造函数，使外界不能创建该类实例
        private Sys_User_BLL()
        {
        }

        /// <summary>
        /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
        /// </summary>
        /// <returns></returns>
        public static Sys_User_BLL GetInstance()
        {
            // 当第一个线程运行到这里时，此时会对locker对象 "加锁"，
            // 当第二个线程运行该方法时，首先检测到locker对象为"加锁"状态，该线程就会挂起等待第一个线程解锁
            // lock语句运行完之后（即线程运行完之后）会对该对象"解锁"
            // 双重锁定只需要一句判断就可以了
            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    // 如果类的实例不存在则创建，否则直接返回
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new Sys_User_BLL();
                    }
                }
            }
            return uniqueInstance;
        }

        #endregion

        #region 分页查询

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable Search(SearchCondition condition, int pageIndex, int pageSize, ref int recordCount)
        {
            int pageCount = 0;
            PagerInfo pager = new PagerInfo();
            pager.Condition = condition;
            pager.CurPage = pageIndex;
            pager.Fields = @"a.id,a.user_name,a.real_name,a.phone,a.branch_code,b.parameter_name as branch_name,a.account_type,c.parameter_name as account_type_name,d.parameter_name as state_name,a.state,
a.last_login_time,a.last_login_ip,a.login_count,a.create_time,a.update_time";
            pager.IsDescending = true;
            pager.PageSize = pageSize;
            pager.SortField = "a.id";
            pager.TableName = @"sys_user a 
JOIN sys_parameter b ON
a.branch_code = b.parameter_code
JOIN sys_parameter c ON
a.account_type = c.parameter_code
JOIN sys_parameter d ON
a.state = d.parameter_code";
            DataTable dt = idal.FindTable(pager, ref recordCount, ref pageCount);
            return dt;
        }




        #endregion

        #region +根据主键获取实体
        /// <summary>
        /// 根据id获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public Sys_User GetById(string id, ref string msg)
        {
            Sys_User model = null;
            try
            {
                string sql = @"SELECT * FROM  `Sys_User`
                                        WHERE id=@id and isdelete='0'";

                model = idal.GetEntityById<Sys_User>(sql, Convert.ToInt32(id));
                msg = "获取成功";
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info($"根据主键获取用户信息异常,异常信息{ex.ToString()}");
            }
            return model;
        }
        #endregion


        #region +逻辑删除

        /// <summary>
        /// 根据主键删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Delete(string id, ref string msg)
        {
            bool success = false;
            try
            {
                string sql = @"UPDATE  `sys_user`  SET   
                                       `isdelete`='1'
                                        WHERE
                                        id=@id  and isdelete='0'";
                success = idal.DeleteEntityById<Sys_User>(sql, Convert.ToInt32(id));
                if (success)
                {
                    msg = "删除成功";
                }
                else
                {
                    msg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info($"逻辑删除用户信息异常,异常信息{ex.ToString()}");
            }
            return success;
        }


        #endregion

    }
}
